/*
 * button.c
 *
 *  Created on: Jan 2, 2024
 *      Author: tangle
 */

#include "button.h"

/* 定义按键，分配内存 */
Button button0;
Button button1;
Button button2;
Button buttonup;

/* 定义句柄 */

static Button *button0_handle  = &button0;
static Button *button1_handle  = &button1;
static Button *button2_handle  = &button2;
static Button *buttonup_handle = &buttonup;

/**
* @name   ButtonTask_Fun
* @brief  FreeRTOS任务函数
* @param  任务参数
* @retval None
*/
void buttontask_fun(void const * argument)
{
    while(1)
    {
        button_ticks();
        osDelay(TICKS_INTERVAL);
    }
}


/**
* @name   button_Init
* @brief  按键初始化
* @param  None
* @retval None
*/

void button_init(void)
{
    /* 连接句柄，head_handle为第一个按键，
       next指针指向下一个按键句柄
       最后一个句柄next指向NULL*/
    head_handle = button0_handle;
    button0_handle->next = button1_handle;
    button1_handle->next = button2_handle;
    button2_handle->next = buttonup_handle;
    buttonup_handle->next = NULL;
    /* 注册回调函数 */
    button_attach(button0_handle,button0_pin_level,button0_attach_fun,BTN_UNPRESS);
    button_attach(button1_handle,button1_pin_level,button1_attach_fun,BTN_UNPRESS);
    button_attach(button2_handle,button2_pin_level,button2_attach_fun,BTN_UNPRESS);
    button_attach(buttonup_handle,buttonup_pin_level,buttonup_attach_fun,BTN_UNPRESS);
}

/**
* @name   button0_attach_fun
* @brief  按键0事件触发回调函数
* @param  事件类型
* @retval None
*/
void button0_attach_fun(PressEvent event)
{
    switch(event)
    {
        case SINGLE_CLICK:



            break;
        case DOUBLE_CLICK:
        	led_flip(&LEDHandle0);

            break;
        case LONG_RRESS_START:

            break;
        case LONG_PRESS_HOLD:

            break;
        default:
            break;
    }
}

/**
* @name   button1_attach_fun
* @brief  按键1事件触发回调函数
* @param  事件类型
* @retval None
*/
void button1_attach_fun(PressEvent event)
{
    switch(event)
    {
        case SINGLE_CLICK:

            break;
        case DOUBLE_CLICK:

            break;
        case LONG_RRESS_START:

            break;
        case LONG_PRESS_HOLD:

            break;
        default:
            break;
    }
}

/**
* @name   button2_attach_fun
* @brief  按键2事件触发回调函数
* @param  事件类型
* @retval None
*/
void button2_attach_fun(PressEvent event)
{
    switch(event)
    {
        case SINGLE_CLICK:

            break;
        case DOUBLE_CLICK:

            break;
        case LONG_RRESS_START:

            break;
        case LONG_PRESS_HOLD:

            break;
        default:
            break;
    }
}

/**
* @name   buttonup_attach_fun
* @brief  按键UP事件触发回调函数
* @param  事件类型
* @retval None
*/
void buttonup_attach_fun(PressEvent event)
{
    switch(event)
    {
        case SINGLE_CLICK:

            break;
        case DOUBLE_CLICK:

            break;
        case LONG_RRESS_START:

            break;
        case LONG_PRESS_HOLD:

            break;
        default:
            break;
    }
}



/* 电平检测回调函数 */
/**
* @name   button0_pin_level
* @brief  按键0电平检测回调函数
* @param  None
* @retval None
*/
uint8_t button0_pin_level(void)
{
    return !HAL_GPIO_ReadPin(KEY0_GPIO_Port,KEY0_Pin);
}

/**
* @name   button1_pin_level
* @brief  按键1电平检测回调函数
* @param  None
* @retval None
*/
uint8_t button1_pin_level(void)
{
    return !HAL_GPIO_ReadPin(KEY1_GPIO_Port,KEY1_Pin);
}

/**
* @name   button2_pin_level
* @brief  按键2电平检测回调函数
* @param  None
* @retval None
*/
uint8_t button2_pin_level(void)
{
    return !HAL_GPIO_ReadPin(KEY2_GPIO_Port,KEY2_Pin);
}

/**
* @name   buttonup_pin_level
* @brief  按键UP电平检测回调函数
* @param  None
* @retval None
*/
uint8_t buttonup_pin_level(void)
{
    return HAL_GPIO_ReadPin(KEY_UP_GPIO_Port,KEY_UP_Pin);
}
